var jweixin = require('jweixin-module');
import { Http } from '@/common/js/http.js'

function OnwxSdk(Curl) {
	return new Promise((resolved, rejected) => {
		var ua = window.navigator.userAgent.toLowerCase();
		var resobj = {} //返回参数
		if (ua.match(/micromessenger/i) == 'micromessenger') {
			let url = ''
			if (Curl) {
				url = 'https://jd.newbeaconhotels.com/h5/jd' + Curl
			} else {
				let pages = getCurrentPages();
				let route = pages[pages.length - 1].route;
				let options = pages[pages.length - 1].options
				let key = Object.keys(options)
				if (key.length > 0) {
					url = 'https://jd.newbeaconhotels.com/h5/jd/' + route + '?' + key[0] + '=' + options[key[0]]
				} else {
					url = 'https://jd.newbeaconhotels.com/h5/jd/' + route
				}
			}
			console.log('签名请求地址：' + url)
			Http({
				url: 'wxSign',
				data: {
					url: url
				}
			}).then(res => {
				jweixin.config({
					debug: false,
					appId: res.data.appid,
					timestamp: res.data.timestamp,
					nonceStr: res.data.nonceStr,
					signature: res.data.signature,
					jsApiList: ['chooseImage', 'getLocation', 'updateAppMessageShareData', 'chooseWXPay', 'scanQRCode']
				});
				jweixin.ready(function(res1) {
					jweixin.error(function(res2) {
						resobj = {
							code: 0,
							msg: '签名失败'
						}
						resolved(resobj)
						return
					});
					resobj = {
						code: 1,
						msg: '成功,可以使用微信SDK了'
					}
					resolved(resobj)
				})
			})
			return true;
		} else {
			uni.showToast({ icon: 'none', title: '请使用微信打开此项目' });
			resobj = {
				code: 0,
				msg: '请使用微信打开此项目'
			}
			resolved(resobj)
		}
	})
}

//定位
function Onlocation() {
	return new Promise((resolved, rejected) => {
		var resobj = {} //返回参数
		jweixin.getLocation({
			type: 'wgs84',
			success: function(res) {
				resobj = {
					code: 1,
					data: res,
					msg: '获取定位成功'
				}
				resolved(resobj)
			},
			fail: function(res) {
				resobj = {
					code: 0,
					msg: '获取定位失败'
				}
				resolved(resobj)
			},
		});
	})
}

//支付
function OnWXPay(e) {
	return new Promise((resolved, rejected) => {
		var resobj = {} //返回参数
		jweixin.chooseWXPay({
			timestamp: e.timeStamp,
			nonceStr: e.nonceStr,
			'package': e.pacKage,
			signType: 'MD5',
			paySign: e.paySign,
			success: function(res) {
				console.log('chooseWXPay成功：' + JSON.stringify(res))
				// 支付成功后的回调函数
				resobj = {
					code: 1,
					msg: '支付成功'
				}
				resolved(resobj)
			},
			fail: function(res) {
				console.log('chooseWXPay失败：' + JSON.stringify(res))
				resobj = {
					code: 0,
					msg: '支付失败'
				}
				resolved(resobj)
			},
			complete: function(res) {
				console.log('chooseWXPay完成：' + JSON.stringify(res))
				if (res.errMsg == 'chooseWXPay:cancel') {
					resobj = {
						code: 3,
						msg: '用户取消支付'
					}
					resolved(resobj)
				}
			}
		});
	})
}

//分享
function OnShare(e) {
	return new Promise((resolved, rejected) => {
		var resobj = {} //返回参数
		jweixin.updateAppMessageShareData({
			title: e.title, // 分享标题
			desc: e.desc, // 分享描述
			link: e.link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
			imgUrl: e.imgUrl, // 分享图标
			success: function(e) {
				resobj = {
					code: 1,
					msg: '分享成功'
				}
				resolved(resobj)
			},
			fail: function(res) {
				resobj = {
					code: 0,
					msg: '分享失败'
				}
				resolved(resobj)
			},
		})
	})
}

//扫一扫
function Oncode(e) {
	return new Promise((resolved, rejected) => {
		var resobj = {} //返回参数
		jweixin.scanQRCode({
			needResult: 0, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
			scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
			success: function(e) {
				var result = e.resultStr; // 当needResult 为 1 时，扫码返回的结果
				resobj = {
					code: 1,
					msg: result
				}
				resolved(resobj)
			},
			fail: function(res) {
				resobj = {
					code: 0,
					msg: '扫码失败'
				}
				resolved(resobj)
			},
		})
	})
}

module.exports = {
	OnwxSdk,
	Onlocation,
	OnWXPay,
	OnShare,
	Oncode
}
